﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Text;
using SDLITS.Common;
using SDLITS.Model;
using MySoft.Data;
namespace SDLITS.Web.SystemMgr
{
    public partial class SysModelMgr : System.Web.UI.Page
    {
        List<Menu> modulesall =SDDbSession.Default.From<Menu>().OrderBy(Menu._.MenuID.Asc).ToList();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DrpBind();
                btnNew.OnClientClick = Window2.GetShowReference("SysModelMgrChange.aspx", "新增模块");
                BindGrid();
            }
        }

        #region BindGrid
        protected void DrpBind()
        {
            List<Menu> modulelist = modulesall.FindAll(delegate(Menu tm) { return tm.ParentID == 0; });
            Drp_Seltop.DataSource = modulelist;
            Drp_Seltop.DataTextField = "MenuName";
            Drp_Seltop.DataValueField = "MenuID";
            Drp_Seltop.DataBind();
            Drp_Seltop.Items.Insert(0, new FineUI.ListItem("--请选择主模块--", "0"));
        }


        protected void SetKey(object sender, EventArgs e)
        {
            string m = "";
            int[] mm = Grid1.SelectedRowIndexArray;
            if (mm.Length > 0)
            {
                foreach (int item in mm)
                {
                    m += Grid1.DataKeys[item][0].ToString() + ",";
                }
               FineUI. PageContext.RegisterStartupScript(Window1.GetShowReference("SysModelPer.aspx?mid=" + m, "新增模块"));
            }
            else
            {
               FineUI. Alert.ShowInTop("请选择一项进行设定！");
            }
        }

        protected void Drpsel(object sender, EventArgs e)
        {
            BindGrid();
        }
        private void BindGrid()
        {
            // 1.设置总项数
            int i = 0;
            List<Menu> list = new List<Menu>();
            //List<EP_Modules> items = TbsDbSession.Default.From<EP_Modules>().Where(EP_Modules._.Topmid == 0).OrderBy(EP_Modules._.Orderid.Asc).ToList();
            //foreach (EP_Modules item in items)
            //{
            //    list.Add(item);
            //    List<EP_Modules> ccitems = TbsDbSession.Default.From<EP_Modules>().Where(EP_Modules._.Topmid == item.Mid).OrderBy(EP_Modules._.Orderid.Asc).ToList();
            //    foreach (EP_Modules ccitem in ccitems)
            //    {
            //        list.Add(ccitem);
            //        i++;
            //        GetItem(list, ccitem.Mid, i);
            //    }
            //}
            if (Drp_Seltop.SelectedValue != "0")
            {
                GetItem1(list, int.Parse(Drp_Seltop.SelectedValue), i);
            }
            else
            {
                GetItem(list, 0, i);
            }
            // 3.绑定到Grid
            Grid1.DataSource = list;
            Grid1.DataBind();
        }

        public void GetItem(List<Menu> cataitem, int topid, int i)
        {
            List<Menu> ccitems = modulesall.FindAll(delegate(Menu tm) { return tm.ParentID == topid; });// TbsDbSession.Default.From<EP_Modules>().Where(EP_Modules._.Topmid == topid).OrderBy(EP_Modules._.Orderid.Asc).ToList();
            foreach (Menu ccitem in ccitems)
            {
                cataitem.Add(ccitem);
                i++;
                GetItem(cataitem, ccitem.MenuID, i);
            }
        }


        public void GetItem1(List<Menu> cataitem, int topid, int i)
        {
            List<Menu> ccitems = null;
            if (i == 0)
            {
                ccitems = modulesall.FindAll(delegate(Menu tm) { return tm.MenuID == topid; });
            }
            else
            {
                ccitems = modulesall.FindAll(delegate(Menu tm) { return tm.ParentID == topid; });
            }

            foreach (Menu ccitem in ccitems)
            {
                cataitem.Add(ccitem);
                i++;
                GetItem1(cataitem, ccitem.MenuID, i);
            }
        }



        protected void Grid1_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }


        #endregion

        #region Events



        #endregion
        protected void Grid1_RowDataBound(object sender, FineUI.GridRowEventArgs e)
        {
            e.Values[1] = "<img src='" + e.Values[0] + "' />&nbsp;&nbsp;" + e.Values[1];
        }

        protected void Grid1_RowCommand(object sender, FineUI.GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                SDDbSession.Default.Delete<Menu>(Menu._.MenuID == id);
              FineUI.  Alert.ShowInTop("删除成功！");
                BindGrid();

            }
        }

    }
}